Skip to content

Conversation

madalinoprea
Copy link
Contributor

Given the following model hierarchy

module Screen
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
  end
end

module Screen
  class Remediation < Screen::ApplicationRecord
    self.abstract_class = true
  end
end

module Screen
  class SctInRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

module Screen
  class SctOutRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

ForestLiana.models will include these child models twice, which will cause Forest
to generate a JSON schema with duplicated fields for the duplicated models.

ForestLiana.models.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
=> [Screen::SctInRemediation (call 'Screen::SctInRemediation.connection' to establish a connection),
 Screen::SctOutRemediation (call 'Screen::SctOutRemediation.connection' to establish a connection),
 Screen::SddInRemediation (call 'Screen::SddInRemediation.connection' to establish a connection)]
❯ cat .forestadmin-schema.json | jq '.collections[24].name'
"Screen__SctInRemediation"

❯ cat .forestadmin-schema.json | jq '.collections[24].fields[] | .field ' | sort
comment"
"created_at"
"created_at"
"id"
"id"
"status"
"status"
"updated_at"
"updated_at"

Given the following model hierarchy

```
module Screen
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
  end
end

module Screen
  class Remediation < Screen::ApplicationRecord
    self.abstract_class = true
  end
end

module Screen
  class SctInRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

module Screen
  class SctOutRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end
```

`ForestLiana.models` will include these child models twice, which will cause Forest
to generate a JSON schema with duplicated fields for the duplicated models.

```ruby
ForestLiana.models.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
=> [Screen::SctInRemediation (call 'Screen::SctInRemediation.connection' to establish a connection),
 Screen::SctOutRemediation (call 'Screen::SctOutRemediation.connection' to establish a connection),
 Screen::SddInRemediation (call 'Screen::SddInRemediation.connection' to establish a connection)]
```

```bash
❯ cat .forestadmin-schema.json | jq '.collections[24].name'
"Screen__SctInRemediation"

❯ cat .forestadmin-schema.json | jq '.collections[24].fields[] | .field ' | sort
comment"
"created_at"
"created_at"
"id"
"id"
"status"
"status"
"updated_at"
"updated_at"
```
@matthv
Copy link
Member

matthv commented Jul 17, 2025

Hello @madalinoprea

Since this was originally proposed, we've changed the implementation from ActiveRecord::Base.subclasses to ActiveRecord::Base.descendants, which should now capture all models in the hierarchy and potentially resolve the duplication issue you were experiencing.
Could you please confirm if you're still encountering duplicate models with the current descendants implementation ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants